//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年4月1日   上午10:54:14
 */
package edu.chinasoft.handler;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import edu.chinasoft.domain.Application;
import edu.chinasoft.domain.Role;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.ApplicationService;
import edu.chinasoft.util.Dom4jUtil;

@Controller("approvalHandler")
@RequestMapping("/approvalHandler")
public class ApprovalHandler {
	private static Logger logger = LoggerFactory.getLogger(ApprovalHandler.class);
	@Autowired
	@Qualifier("applicationService")
	private ApplicationService applicationService;
	//填写请假单
	@RequestMapping(value="/addApplication.do",method= {RequestMethod.POST,RequestMethod.GET})
public String addApplication(Application application,ModelAndView modelAndView, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.info(" ApplicationHandler >>> addApplication start.......");
    //获取用户填写的审批者
    String approvalUser = request.getParameter("selectapprovaluser");
    User user = new User();
    user.setUserName(approvalUser);
    application.setApprovalUser(user);
    //获取当前时间
    Date currentDate = new Date();
    logger.info("当前时间为："+application.getApplicationBegin());
    logger.info("结束时间为："+application.getApplicationEnd());
    //获取当前登录用户
    User userLogin = (User)session.getAttribute("userLogin");
    logger.info("当前登录用户为："+userLogin);
    application.setApplicationUser(userLogin);
    boolean result = applicationService.insertApplication(application);
    if(!result) {
    	return "/addapplicationerror";
    }
    logger.info(" ApplicationHandler >>> addApplication end.......");
	return "redirect:/applicationHandler/showNoSubmitApplication.do";
	}
	//修改请假单
	@RequestMapping(value = "/updateApplicationPage.do",method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView getUserForAddApproval(Integer applicationId,String approvalName,ModelAndView modelAndView, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info(" ApplicationHandler >>> updateApplicationPage start.......");
        logger.info("当前申请单的id为："+applicationId+"，审批者的名字为："+approvalName);
        //获取当前用户的下一级审批者
        //获取当前登录用户
        User userLogin = (User)session.getAttribute("userLogin");
        //获取当前登录用户的角色
        Role loginRole = applicationService.selectRoleByUser(userLogin);
        //获取xml文件的绝对路径
        String path = "/config/flow.xml";
        String realPath = request.getServletContext().getContextPath();
        List<String> roleNameList = Dom4jUtil.getContent(realPath);
        int index = roleNameList.indexOf(loginRole.getRoleName());
        String roleName = roleNameList.get(index+1);
        Role role = new Role();
        role.setRoleName(roleName);
        //获取角色对应的用户
        List<User> userList = applicationService.selectUserByRole(role);
        session.setAttribute("approvalUserInfo", userList);
        logger.info("下一级审批者有："+userList);
        Application application = new Application();
        application.setApplicationId(applicationId);
        //通过id获取当前申请单的其他信息
        Application applicationById = applicationService.selectApplicationById(application);
        logger.info("当前申请单的信息为："+applicationById);
        User user = new User();
        user.setUserName(approvalName);
        applicationById.setApprovalUser(user);
        session.setAttribute("updateapplication", applicationById);
        modelAndView.setViewName("/updateapplication");
        logger.info(" ApplicationHandler >>> updateApplicationPage end.......");
		return modelAndView;
	}
	@InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
}
